package by.airlanterns.service;

import by.airlanterns.dao.UserDaoImpl;
import by.airlanterns.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Sergey
 * Date: 27.01.13
 * Time: 14:40
 * To change this template use File | Settings | File Templates.
 */
@Service
public class UserService {


    @Autowired
    private UserDaoImpl userDao;

    public static final int STATUS_OK = 1;
    public static final int STATUS_LOGIN = 2;
    public static final int STATUS_EMAIL = 3;

    public int saveUser(User user) {

        List<User> users = userDao.selectUserByLogin(user.getLogin());
        if (users.size() > 0) {
            return STATUS_LOGIN;
        } else {
            users = userDao.selectsUserByEmail(user.getEmail());
            if (users.size() > 0) {
                return STATUS_EMAIL;
            } else {
                userDao.save(user);
                return STATUS_OK;
            }
        }

    }

    public User findUserById(int id) {
        return userDao.findById(id);
    }

    public void saveOrUpdate(User user) {
        userDao.saveOrUpdate(user);
    }

    public List<User> findUserByLogin(String login) {
        return userDao.selectUserByLogin(login);
    }

    public List<User> findAllUsers() {
        return userDao.findAll();
    }

    public void deleteUser(User user) {
        userDao.delete(user);
    }

    public User finnUserById(int id) {
        return userDao.findById(id);
    }


}
